var tree = layui.tree
    ,layer = layui.layer
    ,table = layui.table
    ,$ = layui.jquery,form=layui.form,
    tableid=getQueryVariable("tableid");
var data = [{
    title: '所有产品'
    ,id: 1
    ,field: ''
    ,children: []
},{
    title: '一级2'
    ,id: 2
    ,field: ''
    ,children: [{
        title: '二级2-1'
        ,id: 5
        ,field: ''
        ,children: [{
            title: '三级2-1-1'
            ,id: 11
            ,field: ''
        },{
            title: '三级2-1-2'
            ,id: 12
            ,field: ''
        }]
    },{
        title: '二级2-2'
        ,id: 6
        ,field: ''
        ,children: [{
            title: '三级2-2-1'
            ,id: 13
            ,field: ''
        },{
            title: '三级2-2-2'
            ,id: 14
            ,field: ''
            ,disabled: true
        }]
    }]
},{
    title: '一级3'
    ,id: 16
    ,field: ''
    ,children: [{
        title: '二级3-1'
        ,id: 17
        ,field: ''
        ,fixed: true
        ,children: [{
            title: '三级3-1-1'
            ,id: 18
            ,field: ''
        },{
            title: '三级3-1-2'
            ,id: 19
            ,field: ''
        }]
    },{
        title: '二级3-2'
        ,id: 27
        ,field: ''
        ,children: [{
            title: '三级3-2-1'
            ,id: 28
            ,field: ''
        },{
            title: '三级3-2-2'
            ,id: 29
            ,field: ''
        }]
    }]
}];
var data0 = [{
    id: "1",
    contracttype: "text1",
    productimg: "statics/images/layui-logo.jpg",
    number: "3",
    batchno: "333",
    delivery: "333",
    salesunitprice: "333",
    costsubtotal: "333",
    amountsubtotal: "333",
    deliverycost: "333",
    apportionment: "333",
    currency: "333",
}, {
    id: "2",
    contracttype: "text1",
    productimg: "statics/images/layui-logo.jpg",
    number: "3",
    batchno: "333",
    delivery: "333",
    salesunitprice: "333",
    costsubtotal: "333",
    amountsubtotal: "333",
    deliverycost: "333",
    apportionment: "333",
    currency: "333",
}, {id: "3", contracttype: "text1", productimg: "statics/images/layui-logo.jpg", number: "3", batchno: "333",
    delivery: "333",
    salesunitprice: "333",
    costsubtotal: "333",
    amountsubtotal: "333",
    deliverycost: "333",
    apportionment: "333",
    currency: "333",}];
//---公司目录
tree.render({
    elem: '#statisticslist' //默认是点击节点可进行收缩
    ,data: data
    ,onlyIconControl: true  //是否仅允许节点左侧图标控制展开收缩
    ,click: function(obj){
        //判断是否有选中的列，有则取消换现点击列
        var selecteds = $(".selecteds");
        if(typeof (selecteds[0])!="undefined"){
            selecteds.removeClass("selecteds");
        }
        obj.elem[0].classList.add("selecteds");
        //obj.data为点击菜单的数据
        table.reload('companydirectorytableid', {
            data:data0 ,
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: obj.data.id
        }, 'data');
    }
});
table.render({
    elem: '#companydirectory'
    ,data:data0
    ,toolbar: '#toolbarDemo'
    ,cols: [[
        {checkbox: true, fixed: true}
        ,{field: 'productimg', title: '图片', width: 120, align: 'center', templet: '#imgTpl'}
        ,{field:'productcode', width: 120, title:'产品编码', sort: true}
        ,{field:'specificationcoding', width: 120, title:'规格编码', sort: true}
        ,{field:'specification', width: 120, title:'规格说明', sort: true}
        ,{field:'chinesename', width: 120, title:'中文名称', sort: true}
        ,{field:'englishnam', width: 120, title:'英文名称', sort: true}
        ,{field: 'chinesedescription', width: 120, title: '中文描述', sort: true}
        ,{field: 'englishdescription', width: 120, title: '英文描述', sort: true}
        ,{field : 'grossweight',width: 120,title : '毛重',sort : true}
        ,{field : 'netweight',width: 120,title : '净重',sort : true}
        ,{field:'unit', title:'单位', sort: true}
    ]]
    ,id: 'companydirectorytableid'
    ,height: 530
    ,page: true
    , done: function (res, curr, count) {
        hoverOpenImg();//显示大图
    }
});
form.on('submit(submitform)', function(data){
    layer.msg(JSON.stringify(data.field));
    table.reload('companydirectorytableid', {
        data:data0 ,
        page: {
            curr: 1 //重新从第 1 页开始
        }
        ,where: data.field
    }, 'data');
    return false;
});
$("#choose").on("click",function () {
    var companydirectory =   table.checkStatus("companydirectorytableid").data;
    if(companydirectory.length == 0){
        layer.msg("请选择数据")
    }else{
        //保存成功关闭此页面
        var oldData =  parent.table.cache[tableid];
        $.each(companydirectory, function (index, value) {
            oldData.push(companydirectory[index]);
        });
        parent.table.reload(tableid,{data : oldData}); //刷新联系人表格

        //执行接口
        layer.msg("保存成功", {icon: 6});
        setTimeout(function () {
            //保存成功则关闭此页面
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },1000)
    }
});
$("#shutdown").on("click",function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
});

//---自建目录
tree.render({
    elem: '#statisticslists' //默认是点击节点可进行收缩
    ,data: data
    ,onlyIconControl: true  //是否仅允许节点左侧图标控制展开收缩
    ,click: function(obj){
        //判断是否有选中的列，有则取消换现点击列
        var selectedss = $(".selectedss");
        if(typeof (selectedss[0])!="undefined"){
            selectedss.removeClass("selectedss");
        }
        obj.elem[0].classList.add("selecteds");
        //obj.data为点击菜单的数据
        table.reload('statisticstableid', {
            data:data0 ,
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: obj.data.id
        }, 'data');
    }
});
table.render({
    elem: '#selfbuiltdirectory'
    ,data:data0
    ,toolbar: '#toolbarDemos'
    ,cols: [[
        {checkbox: true, fixed: true}
        ,{field: 'productimg', title: '图片', width: 120, align: 'center', templet: '#imgTpl'}
        ,{field:'productcode', width: 120, title:'产品编码', sort: true}
        ,{field:'specificationcoding', width: 120, title:'规格编码', sort: true}
        ,{field:'specification', width: 120, title:'规格说明', sort: true}
        ,{field:'chinesename', width: 120, title:'中文名称', sort: true}
        ,{field:'englishnam', width: 120, title:'英文名称', sort: true}
        ,{field: 'chinesedescription', width: 120, title: '中文描述', sort: true}
        ,{field: 'englishdescription', width: 120, title: '英文描述', sort: true}
        ,{field : 'grossweight',width: 120,title : '毛重',sort : true}
        ,{field : 'netweight',width: 120,title : '净重',sort : true}
        ,{field:'unit', title:'单位', sort: true}
    ]]
    ,id: 'selfbuiltdirectorytableid'
    ,height: 530
    ,page: true
    , done: function (res, curr, count) {
        hoverOpenImg();//显示大图
    }
});
form.on('submit(submitforms)', function(data){
    layer.msg(JSON.stringify(data.field));
    table.reload('selfbuiltdirectorytableid', {
        data:data0 ,
        page: {
            curr: 1 //重新从第 1 页开始
        }
        ,where: data.field
    }, 'data');
    return false;
});

$("#chooses").on("click",function () {
    var selfbuiltdirectory =   table.checkStatus("selfbuiltdirectorytableid").data;
    if(selfbuiltdirectory.length == 0){
        layer.msg("请选择数据");
    }else{
        //执行接口
        var oldData =  parent.table.cache["storageproductstableid"];
        $.each(selfbuiltdirectory, function (index, value) {
            oldData.push(selfbuiltdirectory[index]);
        });
        parent.table.reload('storageproductstableid',{data : oldData}); //刷新银行信息表格
        layer.msg("保存成功", {icon: 6});
        setTimeout(function () {
            //保存成功则关闭此页面
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },1000);
    }
});

$("#shutdowns").on("click",function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
});

function hoverOpenImg() {
    var img_show = null; // tips提示
    $("body").delegate("td img", "mouseover", function () {
        var img = "<img class='img_msg' src='" + $(this).attr('src') + "' style='width:130px;' />";
        img_show = layer.tips(img, this, {
            tips: [2, 'rgba(41,41,41,.5)']
            , area: ['160px']
        });
    });
    $("body").delegate("td img", "mouseout", function () {
        layer.close(img_show);
    });
}

